<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\TaskType;
use App\Models\TaskAudit;
use App\Models\TaskSubmit;
use App\Models\Task;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Requests\SubmitTask;

class TaskController extends Controller
{
    /**
     * 任务首页.
     */
    public function index(Request $request)
    {
        return view('home.task.index');
    }

    public function test()
    {
        $list = TaskType::get(['id', 'name']);
        $list = $list->toArray();

        dump(array_column($list, 'name', 'id'));
        $arr = [1 => 'foo', 2 => 'bar', 'val' => 'Option name'];
        dump($arr);
    }

    /**
     * 获取任务列表.
     *
     * 排序 sort_type
     * 任务分类 task_type.
     */
    public function getList(Request $request)
    {
        $task = new Task();
        $list = $task->get_list($request);

        return $this->ajax_success($list);
    }

    /**
     * 获取任务详情.
     *
     * id task_id
     */
    public function getInfo(Request $request)
    {
        $request->task_id = 1;
        if (empty($request->task_id)) {
            return $this->ajax_error('请选择任务');
        }
        $task = new Task();
        $info = $task->get_info($request);

        return $this->ajax_success($info);
    }

    /**
     * 领取任务
     *
     * id task_id
     */
    public function postTask(Request $request)
    {
        $request->task_id = 1;
        $order_data = Order::where('user_id', $request->user->id)->where('task_id', $request->task_id)->where('status', 0)->first();
        if ($order_data) {
            return $this->ajax_error('任务已领取');
        }

        $time = date('Y-m-d H:i:s', time());
        $task = Task::where('id', $request->task_id)->whereDate('e_time', '>', $time)->first();
        if (empty($task)) {
            return $this->ajax_error('该任务不存在');
        }

        $order = new Order();
        $order->user_id = $request->user->id;
        $order->task_id = $request->task_id;
        $res = $order->save();
        if ($res) {
            return $this->ajax_success('成功');
        } else {
            return $this->ajax_error('失败');
        }
    }
    //任务提交要求
    public function get_submit_step(Request $request)
    {
        $task_id = $request->task_id = 1;
        $user = $request->user;

        $step = new TaskSubmit();
        $task_step = $step->get_submit_step($task_id);
        if(!empty($task_step)){
            return $this->ajax_data($task_step);
        }else{
            return $this->ajax_error('暂无数据');
        }
    }
    //提交任务
    public function submit_task(SubmitTask $request)
    {
        $user = $request->user;
        $detail_arr = [];//待定
        $taskaudit = new TaskAudit();
        $taskaudit->task_id = $request->task_id;
        $taskaudit->user_id = $user->id;
        $taskaudit->detail = json_encode($detail_arr);
        $res = $taskaudit->save();
        if ($res) {
            return $this->ajax_success('成功');
        } else {
            return $this->ajax_error('失败');
        }

    }
}
